# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  include Easel::Menu

  def flash_message
    return unless flash[:notice] || flash[:warn] || flash[:error]

    rvalue =  %{<div id="flash-content">}
    if flash[:notice] 
      rvalue << %{<div id="notice">#{flash[:notice]}</div>}
    end 
    if flash[:warn] 
      rvalue << %{<div id="warn">#{flash[:warn]}</div>}
    end
    if flash[:error] 
      rvalue << %{<div id="error">#{flash[:error]}</div>}
    end 
    rvalue << %{</div>}
    rvalue <<  %{<div style="clear:both; height: 1pt">&nbsp;</div>}
    rvalue
  end

  def my_account_path
    {:controller => :users, :action => :show, :id => current_user_id}
  end

  def left_menu
   <<-HTML
    <div id="main_menu" class="treeview"> 
      #{get_menu}     
    </div>
   HTML
  end

  def use_full_tiny_mce
    return <<-CONTENT
      <script type="text/javascript" src="/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
      <script type="text/javascript">
        tinyMCE.init({
          // General options
          mode : "textareas",
          theme : "advanced",
          plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

          // Theme options
          theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
          theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
          theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,advhr,|,print,|,ltr,rtl,|,fullscreen",
          theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
          theme_advanced_toolbar_location : "top",
          theme_advanced_toolbar_align : "left",
          theme_advanced_resizing : true
        });
      </script>
    CONTENT
  end

  def use_minimal_tiny_mce
    return <<-CONTENT
      <script type="text/javascript" src="/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
      <script type="text/javascript">
        tinyMCE.init({
          // General options
          mode : "textareas",
          theme : "advanced",
          plugins : "pagebreak,style,layer,advhr,advimage,advlink,emotions,inlinepopups,insertdatetime,preview,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
          theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
          theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
          theme_advanced_buttons3 : "sub,sup,|,charmap,advhr,|,print,|,ltr,rtl,|,fullscreen",
          theme_advanced_toolbar_align : "left",
          theme_advanced_toolbar_location : "top"
        });
      </script>
    CONTENT
  end

  def tabs(tab_names)
    tabs = []
    i = 0
    tab_names.each do |tname|
      id = tname.downcase.gsub(/ /,"_")
      style = ""
      style = %(style="color: #ccc;") if i > 0
      tabs << %(<a href="#" id="#{id}_tab" class="tab" onclick="switch_tab('#{id}')" #{style}>#{tname}</a>)
      i += 1
    end
    rvalue = %(<div class="tabs">)
    rvalue << tabs.join("&nbsp;|&nbsp;")
    rvalue << "</div>"
  end

  def content_security_value(model) 
    security = model.content
    if action_name == "show"
      security.user_groups.collect{|ug| ug.name + "<br/>"}
    else
      rvalue = %{<ul id="all_groups" class="checklist">}
      @user_groups_for_user.each_with_index do |grp,i|
        bg = row_class(i)
        input_id = "grp_#{grp.id}"
        checked = ( security && security.user_group_ids.include?(grp.id) ? "checked" : "")
        bg << "_" << checked if checked.length > 0
        rvalue << <<-HTML
          <li class="#{bg}">
            <label id="lbl_#{input_id}" for="#{input_id}" onclick="do_highlight('#{input_id}')">
            <input id="#{input_id}" name="#{input_id}" type="checkbox" #{checked}/>&nbsp;&nbsp;#{grp.name}
            </label>
          </li>
        HTML
      end
      rvalue << "</ul>"
    end
  end

  def links_for_content(model)
    entity = model.presentable_type.tableize
    entity_id = model.presentable_id

    links link_to('Show', "/#{entity}/#{entity_id}"),
          link_to('Edit', "/#{entity}/#{entity_id}/edit"),
          link_to('Destroy',"/#{entity}/#{entity_id}", :confirm => 'Are you sure?', :method => :delete)
  end

  def document_category_documents_value
    rvalue = <<-TABLEDEF
      <table class="document_versions">
        <tr>
          <th>Name</th>
          <th>Version</th>
          <th>Content Type</th>
          <th>Size</th>
        </tr>
    TABLEDEF
    @document_category.documents.each do |doc|
      current_version = doc.current_version
      rvalue << %Q(<tr><td>) 
      if action_name == "show"
        rvalue << link_to_or_show(doc.name, :controller => "documents", :action => "download", :id => doc.id)
      else
        rvalue << link_to(doc.name, edit_document_category_document_path(doc.document_category,doc))
      end
      rvalue << %Q(</td>) 
      rvalue << %Q(<td>#{current_version.version_number.to_s}</td>)
      rvalue << %Q(<td>#{current_version.version_content_type.to_s} </td>)
      rvalue << %Q(<td>#{current_version.kilobytes.to_s}kb</td>)
    end
    rvalue << "</table>"
    unless action_name == "show"
      rvalue << %Q(<div class="actions">)
      rvalue << link_to("Add Document",
        new_document_category_document_path(@document_category.id)) 
      rvalue << %Q(</div>)
    end
    rvalue
  end

  def gallery_images_value
    @gallery.artworks.collect do |art|
      %Q(<div class="art_thumb">#{link_to(image_tag(art.image.url(:thumb)), "/#{art.url}" )}<br/>#{link_to(art.name,"/"+art.url)}</div>)
    end
  end

  def row_class(i)
    ( i % 2 == 0 ) ? "even" : "odd"
  end

end
